new Vue({
    el:"#app",
    data:{
        totalMoney:0,
        productList:[],
        checkAllFlag:false,
        isShow:false,
        curProduct:''
    },
    mounted:function () {
        this.$nextTick(function () {
            this.createView();
        })
    },
    filters:{  //过滤器
        partFilter:function (value,type) {
            return '￥'+value.toFixed(2) + type;
        }
    },
    methods:{
        //绑定数据
        createView:function () {
            //请求json文件数据
            this.$http.get('data/cartData.json',{'id':123}).then(res=>{
                this.productList = res.data.result.list;
            });
        },
        //商品数量加减
        changeMoney:function (item,type) {
            if(type<0){
                item.productQuantity--;
                if(item.productQuantity < 1){
                    item.productQuantity = 1;
                }
            }else {
                item.productQuantity++;
            }
            this.calTotalMoney();
        },
        //单选
        selectProduce:function (item){
            if(typeof item.checked == 'undefined'){
                Vue.set(item,"checked",true);       //注册全局变量
                // this.$set(item,"checked",true);     //注册局部变量
            }else {
                item.checked = !item.checked;
            }
            this.calTotalMoney();
        },
        //计算总金额
        calTotalMoney:function(){
            this.totalMoney = 0;
            this.productList.forEach((item,index)=>{
                if(item.checked){
                    this.totalMoney += item.productQuantity*item.productPrice;
                }
            });
        },
        //全选
        checkAll:function (flag) {
            this.checkAllFlag = flag;
            var _this = this;
            this.productList.forEach(function (item,index) {
                if(typeof item.checked == 'undefined'){
                    _this.$set(item,"checked",_this.checkAllFlag);     //注册局部变量
                }else {
                    item.checked = _this.checkAllFlag;
                }
            });
            this.calTotalMoney();
        },
        //删除
        deleteBtn:function (item) {
            this.isShow = true;
            this.curProduct = item;
        },
        //确认删除
        sureDel:function () {
            this.isShow = false;
            var index = this.productList.indexOf(this.curProduct);
            this.productList.splice(index,1);
            this.calTotalMoney();
        }
    }



});
